package net.minecraft.src;

import net.minecraft.client.Minecraft;

import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

public class GuiSequenzer extends GuiContainer
{
	
    private float currentScroll;
    private boolean isScrolling;
    private boolean wasClicking;

    
    private final int distanceSliderPassageToLeftBorder = 117;
    private final int slotSize = 18;
    private final int slotRowAmount = 18;
    private final int slotsRowsVisible =  6;
    private final int lengthSliderPassage = slotsRowsVisible * slotSize;

    private final int sliderThickness = 12;
    private final int lengthSliderRec = (int)15;

    
    private final  int  distanceToTopBorder =  17;

    private int sliderStartingPoint;
    private int sliderStopingPoint;
    private int sliderEfectiveStartingPoint;

    private int sliderEfectiveStopingPoint;
    
    private int usableSliderLength;
    private TileSequenzer  tileSequenzer;


    public float determineSliderPosition( int  mouseYposition )
    {
	    if(  mouseYposition < sliderEfectiveStartingPoint )
	    {
	        return 0.0F;
	    }
	    else if( mouseYposition > sliderEfectiveStopingPoint )
	    {
	        return 1.0F;
	    }

        return (float)(mouseYposition - sliderEfectiveStartingPoint)/(float)(lengthSliderPassage - lengthSliderRec);
    }


    public GuiSequenzer(InventoryPlayer inventoryplayer, TileSequenzer inTileSequenzer)
    {

        super(new ContainerSequenzer(inventoryplayer, inTileSequenzer));
        currentScroll = 0.0F;
        isScrolling = false;
    	tileSequenzer = inTileSequenzer;
    	inTileSequenzer.inventoryScroled( 0 );

    }

    private void setupSizes()
    {
        sliderStartingPoint = (height - ySize) / 2 + distanceToTopBorder;
        sliderStopingPoint = lengthSliderPassage + sliderStartingPoint;
        sliderEfectiveStartingPoint= sliderStartingPoint + (int)lengthSliderRec/2;

        sliderEfectiveStopingPoint=  sliderStopingPoint - (int)lengthSliderRec/2;
        
        usableSliderLength = sliderEfectiveStopingPoint - sliderEfectiveStartingPoint;	
    	
    }
    protected void drawGuiContainerForegroundLayer()
    {
        fontRenderer.drawString("Sequence", 60, 6, 0x404040);
        fontRenderer.drawString("Frequency", 150, 25, 0x404040);
        
        fontRenderer.drawString("1", 142, 43, 0x404040);
        fontRenderer.drawString("2", 142, 61, 0x404040);
        fontRenderer.drawString("4", 142, 79, 0x404040);
        fontRenderer.drawString("8", 142, 97, 0x404040);
        
     
        int  row = (int)((slotRowAmount - slotsRowsVisible)*currentScroll );
        int distanceFromRight = 14;
        int offset = 8;
        for(  int i = 1 ; i <= slotsRowsVisible ;  ++i  )
        {	
	        fontRenderer.drawString(Integer.toString(i+ row ), distanceFromRight, slotSize * (i-1) + distanceToTopBorder +offset, 0x404040);
        }
    }

    public void drawScreen(int i, int j, float f)
    {
    	//setupSizes();
        boolean flag = Mouse.isButtonDown(0);
        int k = guiLeft;
        int l = guiTop;
        int i1 = k + distanceSliderPassageToLeftBorder;
        int j1 = l + distanceToTopBorder;
        int k1 = i1 + sliderThickness;
        int l1 = j1 + lengthSliderPassage;
        if (!wasClicking && flag && i >= i1 && j >= j1 && i < k1 && j < l1)
        {
            isScrolling = true;
        }
        if (!flag)
        {
            isScrolling = false;
        }
        wasClicking = flag;
        if (isScrolling)
        {
            currentScroll = determineSliderPosition( j );
            tileSequenzer.inventoryScroled( (int)((slotRowAmount - slotsRowsVisible)*currentScroll ) );
           // ((ContainerCreative)inventorySlots).scrollTo(currentScroll);
        }
        super.drawScreen(i, j, f);
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glDisable(2896 /*GL_LIGHTING*/);
    }
    public void onGuiClosed()
    {
    	super.onGuiClosed();

    }
    protected void drawGuiContainerBackgroundLayer(float f, int i, int j)
    {
    	setupSizes();
        int k = mc.renderEngine.getTexture("/mod/sequenzerGuiNew.png");
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        mc.renderEngine.bindTexture(k);
        int l = (width - xSize) / 2;
        int i1 = (height - ySize) / 2;
        drawTexturedModalRect(l, i1, 0, 0, xSize, ySize);
        
        drawTexturedModalRect(
        		  l + distanceSliderPassageToLeftBorder
        		, (int)(sliderStartingPoint + (int)(usableSliderLength* currentScroll) )
        		, 0
        		, ySize
        		, 12
        		, (int)lengthSliderRec);     
    }
}
